#include "uploadform.h"

#include "ui_uploadform.h"

#include <QFileDialog>
#include <UpLoadFile.h>
#include <qmessagebox.h>

UpLoadForm::UpLoadForm(QWidget* parent) : QWidget(parent),
                                          ui(new Ui::UpLoadForm) {
    ui->setupUi(this);
}

UpLoadForm::~UpLoadForm() {
    delete ui;
}

void UpLoadForm::on_btn_select_clicked() {
    QString filename = QFileDialog::getOpenFileName();
    // 处理选择的文件
    ui->btn_get->setFocus();
    ui->lineEdit->setText(filename);
    // m_talk_files.push(filename);
    QPixmap pixmap(filename);
    ui->label_pixmap->setPixmap(pixmap);
}

void UpLoadForm::on_btn_get_clicked() {
    auto upfile = new UpLoadFile(this);

    upfile->put(ui->lineEdit->text());
    connect(upfile, &UpLoadFile::finished, this, &UpLoadForm::onUploadFinished);
}

void UpLoadForm::onUploadFinished(QString url) {
    auto* upFile = static_cast<UpLoadFile*>(sender());

    QMessageBox::about(nullptr, "上传成功", url);
    upFile->deleteLater();
}
